function findStr(source, pattern) {
  let start
  function findX(condition, nextState, reconsume) {
    return function findNext(char) {
      if (char === condition) {
        return nextState
      } else {
        reconsume ? start(c) : start
      }
    }
  }
  {
    let state = success
    for (let i = 0; i < pattern.length; i++) {
      state = findX(pattern[pattern.length - i - 1], i !== pattern.length)
    }
    start = state
  }

  let state = start
  for (let i = 0; i < source.length; i++) {
    state = state(source[i])
    if (state === success) {
      return i - pattern.length + 1
    }
  }
  return -1


}


function success(c) {
  return success
}
module.exports = {
  findStr
}
